// jqgrid表格配置
    // 使jqgrid的宽和高自适应父元素
      var tableHeadFix = ['设备编号','设备名称','设备地址','设备型号','设备类型','工单类型','经度','纬度','施工单位','操作人','实施人',
          '项目名称','产品线','状态','派单时间','完成时间','备注','获取方式','RSRP','SINR','CELLID','故障描述','故障原因','是否恢复',
          "具备安装","原因"];
          var colModelsFix = [
                                              {name:'deviceId',index:'deviceId', sortable: false},
                                              {name:'deviceName',index:'deviceName', sortable: false},
                                              {name:'deviceAddrs',index:'deviceAddrs', sortable: false},
                                              {name:'version',index:'version', sortable: false},
                                               {name:'devicetype',index:'devicetype', sortable: false},
                                              {name:'additionalBusinessId',index:'additionalBusinessId', sortable: false,formatter: businessId},
                                              {name:'longitude',index:'longitude', sortable: false},
                                              {name:'latitude',index:'latitude', sortable: false},
                                              {name:'constructionTeamNames',index:'constructionTeamNames', sortable: false},
                                               {name:'operator',index:'operator', sortable: false},
                                              {name:'builder',index:'builder', sortable: false},
                                              {name:'projectName',index:'projectName', sortable: false},
                                              {name:'productLineName',index:'productLineName', sortable: false},
                                               {name:'state',index:'state', sortable: false,formatter: state},
                                              {name:'assignTime',index:'assignTime', sortable: false},
                                              {name:'endTime',index:'endTime', sortable: false},

                                              {name:'description',index:'description', sortable: false},
                                              {name:'acquisitionMode',index:'acquisitionMode', sortable: false,formatter: acquisitionMode},
                                              {name:'rsrp',index:'rsrp', sortable: false},
                                              {name:'sinr',index:'sinr', sortable: false},
                                              {name:'cellid',index:'cellid', sortable: false},
                                                {name:'faultDesc',index:'faultDesc', sortable: false},
                                                {name:'faultReason',index:'faultReason', sortable: false},
                                                {name:'recove',index:'recove', sortable: false,formatter: recove},
                                                {name:'install',index:'install', sortable: false,formatter: install},
                                                {name:'isInstallDesc',index:'isInstallDesc', sortable: false}
                         ];

    $(function(){
        //页面加载完成之后执行

        pageInit();
    });

  // 日期选择器
    var dataArr = ["#beginTime","#endTime"]
    for(var i = 0, len = dataArr.length; i < len; i++){
    	$(dataArr[i]).jcDate({
    		IcoClass : "jcDateIco",
    		Event : "click",
    		Speed : 100,
    		Left : 0,
    		Top : 28,
    		format : "-",
    		Timeout : 100
    	});
    	}
    	//0='未处理',1='未审核',2='已完成',3='待整改'*/
    function state(state){
           if(state == 0){
               return '未处理';
           }else if(state == 1) {
               return '未审核';
           }else if(state == 2) {
               return '已完成';
           }else if(state == 3) {
               return '待整改';
                             }
       }

      function businessId(additionalBusinessId){
      /**
       group 1 基站勘察
           *    group 2 基站安装
           *    group 3 基站排障
           *    group 4 终端勘察
           *    group 5 终端安装
           *    group 6 终端排障
           *    group 7 监控勘察
           *    group 8 监控安装
           *    group 9 监控排障
      */
      var str ="";
        switch (additionalBusinessId) {
        case 1 : str="基站勘察";break;
        case 2 : str="基站安装";break;
        case 3 : str="基站排障";break;
        case 4 : str="终端勘察";break;
        case 5 : str="终端安装";break;
        case 6 : str="终端排障";break;
        case 7 : str="监控勘察";break;
        case 8 : str="监控安装";break;
        case 9 : str="监控排障";break;
        }
       return str;
      }
   function recove(inarg){
//   console.log("recove==="+ inarg);
       if(inarg == null){
       return "";
       }
      if(inarg){
            return '是';
         }else {
            return '否';
          }
      }
      function install(inarg){
     // console.log("install==="+ inarg);
     if(inarg == null){
            return "";
            }
           if(inarg){
                 return '是';
              }else {
                 return '否';
               }
      }
 function acquisitionMode(inarg){
          if(inarg == null){
             return "";
             }
            if(inarg){
                  return '蓝牙';
               }else {
                  return 'WIFI';
                }

      }

    // 操作按钮
//    function check() {
//        var check = "";
//            check = "<a href='#qualitycontrol-reportGroup-systemReport' class='btn btn-primary btn-sm check' title='查看'>查看</a>";
//        return check;
//    };

//     $(function () {
//            $("#workOrderType").bind("change",function () {
//                if (this.value== 1  ||this.value==2 )
//                 {tableHead=tableHeadFix.concat('获取方式','RSRP','SINR','RSRP','CELLID');
//                  colModels =colModelsFix.concat( {name:'acquisitionMode',index:'acquisitionMode', sortable: false},
//                  {name:'rsrp',index:'rsrp', sortable: false},
//                  {name:'sinr',index:'sinr', sortable: false},
//                  {name:'cellid',index:'cellid', sortable: false});
//
//                 }
//                else if(this.value==3)
//                {
//                  var isHidden = true;
//                tableHead=tableHeadFix.concat('故障描述','故障原因','是否恢复');
//                 colModels =colModelsFix.concat(
//                 {name:'faultDesc',index:'faultDesc', sortable: false, hidden: isHidden},
//                 {name:'faultReason',index:'faultReason', sortable: false},
//                 {name:'isRecove',index:'isRecove', sortable: false}
//                 );
//}
////        $("#reportList").destroy;
//        console.log("changed tableHead :"+tableHead);
//        console.log("changed colModels "+colModels);
//
//          pageInit();
//                })
//            });



    function pageInit(){
        var parentHeight,parentWidth;
        parentHeight = $("#report-table").height() - 23,
        parentWidth = $("#report-table").width() + 14;
//         var workOrderType = $("#workOrderType").val();
//         if (workOrderType = )
        //创建jqGrid组件
//        if(tableHead===undefined){
//        tableHead = tableHeadFix;
//        colModels =colModelsFix;
//        }
//        console.log("tableHead :"+tableHead);
//        console.log("colModels "+colModels);
        jQuery('#reportList').GridUnload();
        jQuery("#reportList").jqGrid({
            url: contextPath + "/SystemReport/querySubTaskReportJqg",
            datatype: "json",
            height: parentHeight,
            width: parentWidth,
             destroy: true,
            colNames:tableHeadFix,
            colModel:colModelsFix,
            sortname:'',
            sortorder:'desc',
            viewrecords: true,
            rowNum: 20,
            rowList:[20,50,100],
            pager:"#gridpager"
        });
        /*创建jqGrid的操作按钮容器*/
        jQuery("#reportList").jqGrid('navGrid', '#gridpager', {edit: false,add: false, del:false,search: false});
    };
    //函数：获取尺寸　　
    var timer = 0;
    $(window).resize(function(){
        clearTimeout(timer);
        timer = setTimeout(function() {
            $("#reportList").setGridWidth($('#report-table').width() + 12);
            $("#reportList").setGridHeight($('#report-table').height() - 23);
        }, 260);
    });　　
    // 侧边栏缩小时，实时的给jqgrid的宽和高赋值
    $("#asidebarSwitch").on('click',function() {
        var time = 0;
        var set = setInterval(function (){
            $("#reportList").setGridWidth($('#report-table').width()*1 + 14);　　
            $("#reportList").setGridHeight($('#report-table').height()*1 - 23);
            time++;
            if(time == 201){
                clearInterval(set);
            }
        },time);
    });

// 查询区域
    // 查询所有产品线
    $.ajax({
        url: contextPath + "/ProductLine/queryAllProductLine",
        type: 'GET',
        dataType: "json",
        contentType: "application/json",
        success: function(data){
            var str = '<option value="" selected>选择产品线</option>';
            for (var i =0 ,len = data.length; i < len; i++) {
            	str += '<option value="' + data[i].id + '">' + data[i].name + "</option>";
            }
            $("#productLineId").html(str);
        },
        error: function(data){

            toastr.error(data.message, "请求失败");
        }
    });
//    // 查询设备类型
//    $.ajax({
//        url: contextPath + "/DeviceType/queryAllDeviceType",
//        type: 'GET',
//        dataType: "json",
//        contentType: "application/json",
//        success: function(data){
//            var str = '<option value="" selected>选择设备类型</option>';
//            for (var i =0 ,len = data.length; i < len; i++) {
//            	str += '<option value="' + data[i].id + '">' + data[i].name + "</option>";
//            }
//            $("#deviceTypeId").html(str);
//        },
//        error: function(data){
//            toastr.error(data.message, "请求失败");
//        }
//    });

    // 查询
    $('#query').on('click',function(){

        var _productLineId = $('#productLineId').val()?Number($('#productLineId').val()):null;
        var projectName = $("#projectName").val();
        var constructionTeamName = $("#constructionTeamName").val();
        var workOrderType = $("#workOrderType").val();
//        var workOrderState = $("#workOrderState").val();
        var beginTime = $("#beginTime").val();
        var endTime = $("#endTime").val();

        $("#reportList").setGridParam({
            url : contextPath + "/SystemReport/querySubTaskReportJqg",
            postData : {
                queryCond: {
                    'productLineId': _productLineId,
                    'projectName': projectName,
                   'constructionTeamName': constructionTeamName,
                    'beginTime': beginTime,
                    'endTime': endTime,
                    'workOrderType': workOrderType
                }
            },
            page: 1
        }).trigger("reloadGrid");
    });

    function buildURL(url,param/*链接和参数*/){
        var paramStr = '';
        $.each(param,function(key,item){
            var link = '&' + key + "=" + item;
            paramStr += link;
        })
        paramStr = url + "?" + paramStr.substr(1);
        return paramStr.replace(' ','');
    }

    // 导出
    $('#export').on('click',function(){
               var productLineId = $("#productLineId").val();
               var projectName = $("#projectName").val();
               var constructionTeamName = $("#constructionTeamName").val();
               var workOrderType = $("#workOrderType").val();

               var beginTime = $("#beginTime").val();
               var endTime = $("#endTime").val();
        var obj = new Object();
        obj.productLineId = productLineId;
        obj.projectName = projectName;
        obj.constructionTeamName = constructionTeamName;
        obj.workOrderType = workOrderType;
        obj.beginTime=beginTime;
        obj.endTime=endTime;
        var url = buildURL("SystemReport/exportreport",obj);
        window.open(url);
    });

    // 查看
    // 获取工单数据
    $("#reportList").on('click','.check',function(){
        var id = $(this).parent().parent().attr('id');
        var reportData = $("#reportList").jqGrid('getRowData',id);
        console.log(reportData.workOrderId);
        $.cookie('reportId',JSON.stringify(reportData.workOrderId));
    });

